/**
 * 
 */
package com.edocbase.desktop.plugins.mdiary;

import static com.edocbase.desktop.plugins.mdiary.MDiaryActionConstants.MDairyActionID.browser_backward_action;
import static com.edocbase.desktop.plugins.mdiary.MDiaryActionConstants.MDairyActionID.browser_forward_action;
import static com.edocbase.desktop.plugins.mdiary.MDiaryActionConstants.MDairyActionID.browser_refresh_action;
import static com.edocbase.desktop.plugins.mdiary.MDiaryActionConstants.MDairyActionID.browser_stop_action;

import com.edocbase.desktop.lightweight.DesktopTabGroup;
import com.edocbase.desktop.lightweight.widgets.TabItem;
import com.edocbase.desktop.plugins.control.RichBrowserTabItem;
import com.edocbase.desktop.plugins.mdiary.MDiaryActionConstants.MDairyActionID;
import com.edocbase.desktop.swt.widgets.RichBrowser;

/**
 * @author X61
 * 
 */
public class RichBrowserButtonObserver implements ITabItemButtonObserver {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.edocbase.desktop.plugins.mdairy.ITabItemButtonObserver#active(com
	 * .edocbase.desktop.lightweight.widgets.TabItem,
	 * com.edocbase.desktop.plugins.mdairy.ToolButtonManager)
	 */
	@Override
	public void active(TabItem item, ToolButtonManager buttonManager) {
		ButtonComposite[] buttons = getButtons(buttonManager);
		for (int i = 0; i < buttons.length; i++) {
			ButtonComposite bc = buttons[i];
			bc.setEnabled(true);
		}
		if (item instanceof RichBrowserTabItem){
			RichBrowserTabItem richItem = (RichBrowserTabItem)item;
			RichBrowser browser = richItem.getRichBrowser();
			ButtonComposite btnC = buttonManager.getButton(MDairyActionID.browser_backward_action.name());
			btnC.setEnabled(browser.isBackEnabled());
			btnC = buttonManager.getButton(MDairyActionID.browser_forward_action.name());
			btnC.setEnabled(browser.isForwardEnabled());
		}
		DesktopTabGroup group = buttonManager.getGroup(MDiaryActionConstants.BROWSER_GROUP_ID);
		group.setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.edocbase.desktop.plugins.mdairy.ITabItemButtonObserver#deactive(com
	 * .edocbase.desktop.lightweight.widgets.TabItem,
	 * com.edocbase.desktop.plugins.mdairy.ToolButtonManager)
	 */
	@Override
	public void deactive(TabItem item, ToolButtonManager buttonManager) {
		ButtonComposite[] buttons = getButtons(buttonManager);
		for (int i = 0; i < buttons.length; i++) {
			ButtonComposite bc = buttons[i];
			bc.setEnabled(false);
		}
		DesktopTabGroup group = buttonManager.getGroup(MDiaryActionConstants.BROWSER_GROUP_ID);
		group.setVisible(true);
	}

	protected ButtonComposite[] getButtons(ToolButtonManager buttonManager) {
		return buttonManager.getButtons(browser_backward_action.name(),
				browser_forward_action.name(), browser_stop_action.name(),
				browser_refresh_action.name());
	}
}
